// index.js
Page({
  data: {
    searchValue: ''
  },
  onInput: function (e) {
    this.setData({ searchValue: e.detail.value });
  },
  handleSearchResults: function (results) {
    this.setData({ searchResults: results });
  },
  data: {
    searchValue: '',
    searchHistory: []
    
  },
  onHistoryTap: function (e) {
    const historyValue = e.currentTarget.dataset.item;
    this.setData({ searchValue: historyValue });
    this.onSearch();
  },
  data: {
    searchValue: '',
    searchHistory: []
  },
  onSearch: function () {
    const searchValue = this.data.searchValue;
    if (searchValue) {
      wx.showToast({ title: '搜索中...', icon: 'loading' });
      setTimeout(() => {
        wx.showToast({ title: '搜索完成', icon: 'success' });
        const searchHistory = this.data.searchHistory;
        if (!searchHistory.includes(searchValue)) {
          searchHistory.unshift(searchValue);
          this.setData({ searchHistory: searchHistory.slice(0, 5) }); 
        }
      }, 1000);
    } else {
      wx.showToast({ title: '请输入搜索内容', icon: 'none' });
    }
  },
  onHistoryTap: function (e) {
    const historyValue = e.currentTarget.dataset.item;
    this.setData({ searchValue: historyValue });
    this.onSearch();
  },
  onClear: function () {
    this.setData({ searchValue: '' });
  },

  data: {
    list: [],
    loading: true // 添加加载状态
  },
  
  onLoad() {
    wx.showLoading({ title: '加载中...' }) // 显示加载提示
    
    wx.cloud.database().collection('rmzytj')
      .get()
      .then(res => {
        console.log('数据获取成功', res)
        this.setData({
          list: res.data,
          loading: false
        })
        wx.hideLoading()
      })
      .catch(err => {
        console.error('数据获取失败', err)
        this.setData({ loading: false })
        wx.hideLoading()
        wx.showToast({ title: '加载失败', icon: 'error' })
      })
  },
})
